<?php
/*
 * 加盟商管理控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ShopManageController extends AdminController {
	/*
	 * 加盟商列表
	 * 
	 */	
	public function index(){
		$page=I('get.page','1','strip_tags');
        $count=M('shop_user')->where('isdel=0')->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=M('shop_user')->where('isdel=0')
		                 ->limit($pagearr['start'],$pagearr['pagesize'])
		                 ->order('shopid desc')->select();

		                
		$this->assign('shops',$result);
		$this->assign('pagearr',$pagearr);
		$this->display('index');
	}
	/*
	 ×添加加盟商
	 */
	public function addShop(){
		

		$this->display();
	}

	/*
	 *保存加盟商
	 */
	public function saveShop(){
		//接收参数
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['connectname']=I('post.connectname','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['username']=I('post.username','','strip_tags');
		$data['showname']=I('post.showname','','strip_tags');
		$data['umobile']=I('post.umobile','','strip_tags');
		$data['uemail']=I('post.uemail','','strip_tags');
		$data['uaddress']=I('post.uaddress','','strip_tags');
		$data['password']=md5(I('post.upassword','','strip_tags'));
		
		$data['createtime']=time();
		$data['founder']=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
		
		
		//保存用户
		$shop_user=M('shop_user');
		
	   $shopid=$shop_user->add($data);///基本信息到加盟商表
	  
	   
	   if($shopid>0 )
	   {
	   
	   	echo "<script type='text/javascript'>alert('添加成功'); window.location.href='./index';</script>";
	   }else
	   {
	   	 
	   	echo "<script type='text/javascript'>alert('添加失败'); window.history.go(-1);window.location.reload();</script>";

	   }
	}
	/*
	 ×编辑加盟商
	 @param shopid 商家id
	 */
	public function editShop(){
			$shopid=I('get.shopid','','strip_tags');
			//获取此商家的信息
			$shop_user=M('shop_user');
			$shop=$shop_user->where('shopid=%d AND isdel=%d',$shopid,0)->select();
			;
			
			$this->assign('shop',$shop[0]);
			
			$this->display();
	}
	/*
	 *保存修改后的加盟商
	 *@param shopid 商家id
	 */
	public function updateShop(){

		//接受参数
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['connectname']=I('post.connectname','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['username']=I('post.username','','strip_tags');
		$data['showname']=I('post.showname','','strip_tags');
		$data['umobile']=I('post.umobile','','strip_tags');
		$data['uemail']=I('post.uemail','','strip_tags');
		$data['uaddress']=I('post.uaddress','','strip_tags');
		$data['upassword']=I('post.upassword','','strip_tags');
		$data['createtime']=time();
		$data['founder']=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
		///$data['urole']=I('post.urole','','strip_tags');
		$data['cateid']=I('post.cateid','','strip_tags');//企业类型
		$data['shopid']=I('post.shopid','','strip_tags');
		
		//判断密码是否修改过
		$shop_user=M('shop_user');
		$devide=M('device');
		$shoppassword= $shop_user->where("shopid='%d'",$_GET['shopid'])->getField('password');
		if($data['upassword'] !=$shoppassword){
			$data['password']=md5($data['upassword']);
		}
         $shop_user->startTrans();
         $result=$shop_user->where('shopid=%d',$data['shopid'])->save($data);
         $map['devicecode']=I('post.devicecode','','strip_tags');
         $devide->where('shopid=%d',$_GET['shopid'])->save($map);
         if($result!==false && $devide!==false)
         {
         	$shop_user->commit();
         	//echo "<script type='text/javascript'>alert('修改成功'); window.location.href='./index';</script>";
         	$this->redirect("ShopManage/index");

         }else
         {
         	$shop_user->rollback(); 
         	//echo "<script type='text/javascript'>alert('修改失败'); window.location.href='./index';</script>";
         	echo "<script>alert('活动删除成功');location.href='/index.php/Admin/ShopManage/editShop'</script>";

         }

		
		
	} 


	/**
	 * 检测设备的唯一
	 */
	public function unique(){

		$where['devicecode']=$_GET['devicecode'];
		$code=M('device')->where($where)->getField("shopid");
		if(empty($code)){
			$data = array('status'=>200,'code'=>'error','info'=>'可用的设备号');
 			echo json_encode($data);
		}else{
			$data = array('status'=>404,'code'=>'error','info'=>'该设备标识已经使用');
 			echo json_encode($data);

		}
		

	}
}